﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace ShanNiang.Mvvm.Converters
{
    [ValueConversion(typeof(string), typeof(Visibility))]
    public class BottleCodeToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="value">传入值</param>
        /// <param name="targetType">传入类型</param>
        /// <param name="parameter">传入参数，类型为 bool? </param>
        /// <param name="culture"></param>
        /// <returns>
        /// 当 <paramref name="value"/> 有内容, <paramref name="parameter"/> 为null 返回  Visibility.Collapsed， 为 true 返回Visibility.Visible 为 false 返回 Visibility.Hidden
        /// 当 <paramref name="value"/> 为NULL  返回 Visibility.Collapsed 为 String.Empty 返回 Visibility.Hidden
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var reversal = System.Convert.ToBoolean(parameter);
            if (value != null)
            {
                if (value is string)
                {
                    var valueStr = (string)value;
                    if (!string.IsNullOrEmpty(valueStr))
                    {
                        return !reversal ? Visibility.Visible : Visibility.Hidden;
                    }
                    return reversal ? Visibility.Visible : Visibility.Hidden;
                }
            }
            return !reversal ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
